package org.example.study.sc.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.example.study.sc.dto.SecKillMsg;
import org.example.study.sc.rabbitmq.config.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: rabbitmq-study
 * @description:
 * @Version V1.0.0
 * @author: dazhi
 * @create: 2021-02-02 11:28
 **/
@Slf4j
@Component
public class OrderSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendSecKillMsg(SecKillMsg secKillMsg) {
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_ORDER_DIRECT, RabbitConfig.QUEUE_ORDER_SECKILL, secKillMsg);
    }

    /***
     * 功能描述: 延迟队列（死信队列）
     * @Param orderId 秒杀订单Id
     * @Param delayTimes 消息延迟毫秒值
     * @author: dazhi
     * @date: 2021/2/3
     */
    public void ttlOrderCancelMsg(Long orderId, final long delayTimes) {
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_TTL_ORDER_DIRECT,
                RabbitConfig.QUEUE_TTL_ORDER, orderId, message -> {
                    // 给消息设置延迟时间，延迟时间一到立马取消
                    message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                    return message;
                });

    }
}
